<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\system;

use think\App;
use app\controller\property\Base;
use app\validate\system\SystemPactValidate;
use app\common\repositories\system\SystemPactRepository;

class SystemPact extends Base
{
    protected $repository;

    public function __construct(App $app, SystemPactRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * 协议列表
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-23 16:48
     */
    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'pact_type' => '',
                'keywords' => '',
            ]);
            [$page, $limit] = $this->getPage();
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        return $this->fetch('system/pact/list', [
            'pactType' => $this->repository::PACT_TYPE,
            'addAuth' => property_auth('propertySystemPactAdd'),
            'editAuth' => property_auth('propertySystemPactEdit'),
            'delAuth' => property_auth('propertySystemPactDel')
        ]);
    }

    /**
     * 添加协议
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-23 16:47
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'pact_type' => '',
                'content' => '',
            ]);
            validate(SystemPactValidate::class)->scene('add')->check($param);
            try {
                if ($this->repository->propertyFieldExists($this->request->propertyId, 'pact_type', $param['pact_type'])) {
                    return $this->error('该类型协议已存在');
                }
                $param['content'] = htmlspecialchars($param['content']);
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加协议失败', $e);
                return $this->error('网络错误');
            }
        } else {
            $typeIds = $this->repository->getSearch([])
                ->where('property_id', $this->request->propertyId)
                ->column('pact_type');
            $pactTypes = $this->repository::PACT_TYPE;
            foreach ($pactTypes as $k => $v) {
                if (in_array($k, $typeIds)) {
                    unset($pactTypes[$k]);
                }
            }
            return $this->fetch('system/pact/add', [
                'pactType' => $pactTypes
            ]);
        }
    }

    /**
     * 编辑协议
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-23 16:48
     */
    public function edit()
    {
        $id = $this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'content' => '',
            ]);
            validate(SystemPactValidate::class)->scene('edit')->check($param);
            try {
                $param['content'] = htmlspecialchars($param['content']);
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('修改协议失败', $e);
                return $this->error('网络失败');
            }
        }
        return $this->fetch('system/pact/edit', [
            'info' => $info
        ]);
    }

    /**
     * 删除协议
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-01-23 16:47
     */
    public function del()
    {
        $id = (int)$this->request->param('id');
        $res = $this->repository->batchDelete([$id], $this->request->userId);
        if ($res) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }
}